require "common"
local LCBHMapping = {"SF|","RFSF|","SFC|","DXF|","HH|"}
local PosMapping = { ["3*3"] = 2, ["4*6"] = 2, ["5*10"] = 2, ["6*15"] = 2, ["4*4"] = 3, ["6*20"] = 3, ["5*5"] = 4, ["7*35"] = 4, ["8*70"] = 4, ["6*6"] = 5, ["7*21"] = 5, ["7*7"] = 6, ["8*8"] = 7, ["8*28"] = 6, ["7*21"] = 5, ["8*56"] = 5}
local MaxPower = 99

function just_str(butType, str)
	if butType == '1' or butType == '2' or butType == '4' then
		if not string.find("3/0",str) then return nil end
	elseif butType == '3' then
		if not string.find("11/12/13/14/15/16/01/02/03/04/05/06",str) then return nil end
	end

	return true
end

function Compute(pnInput, nTotal, nSelect)
	local i
	local result = 0
	local temp = {}

	if nTotal < nSelect then
		return 0
	elseif nSelect == 1 then
		for i = 1, nTotal do
			result = result + pnInput[i]
		end
	elseif nTotal == nSelect then
		result = 1
		for i = 1, nTotal do
			result = result * pnInput[i]
		end
	elseif nTotal > nSelect then
		for i = 2, #pnInput do
			temp[i - 1] = pnInput[i]
		end
		result = pnInput[1] * Compute(temp,nTotal - 1, nSelect - 1) + Compute(temp,nTotal - 1, nSelect)
	end

	return result
end



function ComputeMoney(pnInput, nTotal, nSelect)
        local i
        local result = 0
        local temp = {}
        local sp = 0
		local tempInput = {}

		for i = 1, #pnInput do
			tempInput[i] = pnInput[i]
		end

        if nSelect == 1 then
                for i = 1, nTotal do
					if pnInput[i] == -1 then
						tempInput[i] = 0
					end
					sp =sp + tempInput[i]
                end
                result =  sp * 2 * 0.65
        elseif nTotal == nSelect then
               sp = 1
                for i = 1, nTotal do
					if pnInput[i] == -1 then
						tempInput[i] = 0
					elseif pnInput[i] == 0 then
						tempInput[i] = 1
					end

					sp = sp * tempInput[i]
                end
				result = sp * 2 * 0.65
        elseif nTotal > nSelect then
                for i = 2, #pnInput do
                        temp[i - 1] = pnInput[i]
                end
				if pnInput[1] == 0 then
					tempInput[1] = 1
				elseif pnInput[1] == -1 then
					tempInput[1] = 0
				end
                result =tempInput[1] * ComputeMoney(temp,nTotal - 1, nSelect - 1) + ComputeMoney(temp,nTotal - 1, nSelect)

        end


        return result
end

function ComputeJCZQMoney(pnInput, nTotal, nSelect,vSelect)
        local i
        local result = 0
        local temp = {}
        local sp = 0
		local tempInput = {}
		local tmpresult = 0
		local vTmpSelect = {}
		for i = 1, #pnInput do
			tempInput[i] = pnInput[i]
		end

        if nSelect == 1 then
			for i = 1, nTotal do
				if pnInput[i] == -1 then
					tempInput[i] = 0
				end
				tmpresult = tempInput[i]
				for j = 1, #vSelect do
					tmpresult = tmpresult * vSelect[j]
				end
				tmpresult = tmpresult * 2
				if tmpresult > 200000 and #vSelect + 1 >= 2 and #vSelect + 1 <= 3 then
					result = result + 200000
				elseif tmpresult > 500000 and #vSelect + 1 >= 4 and #vSelect + 1 <= 5 then
					result = result + 500000
				elseif tmpresult > 1000000 and #vSelect + 1 >= 6 then
					result = result + 1000000
				elseif tmpresult > 0  and tmpresult < 2 then
					result = result + 2
				else
					result = result + tmpresult
				end
			end
        elseif nTotal == nSelect then
               tmpresult = 1
                for i = 1, nTotal do
					if pnInput[i] == -1 then
						tempInput[i] = 0
					elseif pnInput[i] == 0 then
						tempInput[i] = 1
					end
					tmpresult = tmpresult * tempInput[i]
                end
				for i = 1, #vSelect do
					tmpresult = tmpresult * vSelect[i]
				end
				tmpresult = tmpresult * 2
				if tmpresult > 200000 and nSelect + #vSelect >= 2 and nSelect + #vSelect <= 3 then
					result = result + 200000
				elseif tmpresult > 500000 and nSelect + #vSelect >= 4 and nSelect + #vSelect <= 5 then
					result = result + 500000
				elseif tmpresult > 1000000 and vSelect + #vSelect >= 6 then
					result = result + 1000000
				elseif tmpresult > 0  and tmpresult < 2 then
					result = result + 2
				else
					result = result + tmpresult
				end
        elseif nTotal > nSelect then
                for i = 2, #pnInput do
                        temp[i - 1] = pnInput[i]
                end
				if pnInput[1] == 0 then
					tempInput[1] = 1
				elseif pnInput[1] == -1 then
					tempInput[1] = 0
				end
				k = #vSelect

				for i = 1, #vSelect do
					vTmpSelect[i] = vSelect[i]
				end
				vTmpSelect[#vSelect + 1] = tempInput[1]
                result =ComputeJCZQMoney(temp,nTotal - 1, nSelect - 1, vTmpSelect) + ComputeJCZQMoney(temp,nTotal - 1, nSelect,vSelect)


        end


        return result
end


function check_jclq(butType, inStr)
	local butNum = 0
	local butPower
	local butStr = ""
	local itemList = {}
	local dhItem = {}  		--逗号,
	local sxItem = {}   	--竖线|
	local jhItem = {}		--右尖括号>
	local xxItem = {}  		-- 斜线
	local equalItem = {} 	--等号 =
	local nInput = {}
	local nTotal = 0
	local m					--过关组合m*n
	local n
	local pos = 0
	local flag = 0
	local fhItem = {}		--分号
	local cancelItem = {}
	local cancelInput = {}
	local cancelButNum = 0
	local oriStr = {}


	if string.find(inStr, "null") ~= nil then return 0 end

	fhItem = Split(inStr, ";")
	itemList = Split(fhItem[1], "#B" )
	if fhItem[2] ~= nil then
		cancelItem = Split(fhItem[2], ",")
	end
	itemLen = table.getn( itemList )

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower > MaxPower or butPower < 1 then return 0 end

	sxItem = Split(itemList[1],"|")
	dhItem = Split(sxItem[1],",")
	nTotal = table.getn(dhItem)

	---
	if butType == '1' or butType == '2' or butType == '3' or butType == '4' then
		k = 1
		for i = 1, #dhItem do
			equalItem = Split(dhItem[i], "=")
			if #equalItem ~= 2 then return 0 end
			xxItem = Split(equalItem[2],"/")
			nInput[i] = table.getn(xxItem)
			if butType == '1'  and (#xxItem > 2 or #xxItem < 1) then return 0 end
			if butType == '2' and (#xxItem > 2 or #xxItem < 1) then return 0 end
			if butType == '3' and (#xxItem > 12 or #xxItem < 1) then return 0 end
			if butType == '4' and (#xxItem > 2 or #xxItem < 1) then return 0 end
			for j = 1,#xxItem do
				if not just_str(butType,xxItem[j]) then return 0 end
			end
			for j = 1,#cancelItem do
				if equalItem[1] == cancelItem[j] then
					cancelInput[k] = nInput[i]
					k = k + 1
				end
			end
		end
		if sxItem[2] == '1*1' then
			singleStr = Split(sxItem[1],",")
			for i  = 1, #singleStr do
					butStr = butStr..LCBHMapping[tonumber(butType)]..singleStr[i]..'|'..sxItem[2]
				if i < #singleStr then
					butStr = butStr ..';'
				end
			end
		else
			butStr = LCBHMapping[tonumber(butType)]..itemList[1]
		end
	elseif butType == '5' then
		k = 1
		for i = 1, #dhItem do
			jhItem = Split(dhItem[i], ">")
			if jhItem[1] == 'SF' or jhItem[1] == 'RFSF' or jhItem[1] == 'DXF' then
				butType = '1'
			else
				butType = '3'
			end
			equalItem = Split(jhItem[2], "=")
			if #equalItem ~= 2 then return 0 end
			xxItem = Split(equalItem[2],"/")
			nInput[i] = table.getn(xxItem)
			if butType == '1'  and (#xxItem > 2 or #xxItem < 1) then return 0 end
			if butType == '2' and (#xxItem > 2 or #xxItem < 1) then return 0 end
			if butType == '3' and (#xxItem > 12 or #xxItem < 1) then return 0 end
			if butType == '4' and (#xxItem > 2 or #xxItem < 1) then return 0 end
			for j = 1,#xxItem do
				if not just_str(butType,xxItem[j]) then return 0 end
			end
			for j = 1,#cancelItem do
				if equalItem[1] == cancelItem[j] then
					cancelInput[k] = nInput[i]
					k = k + 1
				end
			end
			butType = '5'
		end
		if sxItem[2] == '1*1' then
			singleStr = Split(sxItem[1],",")
			for i  = 1, #singleStr do
					butStr = butStr..LCBHMapping[tonumber(butType)]..singleStr[i]..'|'..sxItem[2]
				if i < #singleStr then
					butStr = butStr ..';'
				end
			end
		else
			butStr = LCBHMapping[tonumber(butType)]..itemList[1]
		end
	end


	xItem = Split(sxItem[2], "*")
	m = tonumber(xItem[1])
	n = tonumber(xItem[2])
	if butType == '1'  and m > 8 then return 0 end
	if butType == '2'  and m > 8 then return 0 end
	if butType == '3'  and m > 4 then return 0 end
	if butType == '4'  and m > 8 then return 0 end

	temp = 0
	pos = PosMapping[sxItem[2]]
	flag = 1
	if pos == nil then
		flag = 0
		for i = 1, m do
			temp = temp + combination(m, m + 1 - i)
			if temp == n then
				pos = m + 1 - i
				break
			end
		end
	end

        if(sxItem[2] == '6*35') then
		pos = 2
		m = 3
	elseif(sxItem[2] == '6*50') then
		pos = 2
		m = 4
	elseif(sxItem[2] == '5*20') then
		pos = 2
		m = 3
	end

	if flag == 0 then
		for i = pos, m do
			butNum = butNum + Compute(nInput, nTotal, i)
		end
		nTotal = #cancelInput
		if nTotal > m then
			nTotal = m
		end
		for i = pos, nTotal do
			cancelButNum = cancelButNum + Compute(cancelInput, #cancelInput, i)
		end
	elseif flag == 1 then
		butNum = butNum + Compute(nInput, nTotal, pos)
		if #cancelInput >=  pos then
			cancelButNum = cancelButNum + Compute(cancelInput, #cancelInput, pos)
		end
	end

	butNum = butNum - cancelButNum
	butStr = butStr..'#B'..butPower



	return butNum, butPower, butStr, butStr
end

function equalSplit(inStr)
	local dhItem = {}
	local tempItem = {}
	local outStr = {}
	local sp = {}
	local sxItem = {}

	dhItem = Split(inStr,",")
	for	i = 1,#dhItem do
		outStr[i] = {}
		tempItem = Split(dhItem[i],"=")
		outStr[i][1] = tempItem[1]
		outStr[i][2] = tempItem[2]
	end
	return outStr
end

function computeSFC(point)
	if point >= 1 and point <= 5 then
		return "01"
	elseif point >= 6 and point <= 10 then
		return "02"
	elseif point >= 11 and point <= 15 then
		return "03"
	elseif point >= 16 and point <= 20 then
		return "04"
	elseif point >= 21 and point <= 25 then
		return "05"
	elseif point >= 26  then
		return "06"
	elseif point >= -5 and point <= -1 then
		return "11"
	elseif point >= -10 and point <= -6 then
		return "12"
	elseif point >= -15 and point <= -11 then
		return "13"
	elseif point >= -20 and point <= -16 then
		return "14"
	elseif point >= -25 and point <= -21 then
		return "15"
	elseif point <= -26 then
		return "16"
	end
end

function query_jclq(butType, inStr, bingoStr)

	local equalLotItem = {{}}
	local equalBingoItem = {{}}
	local dhItem = {}
	local tempItem = {}
	local pos = {}
	local sxItem = {}
	local Sp = {}
	local Host = {}
	local Guest = {}
	local concedePoint = {}
	local wholePoint = {}
	local temp = {}
	local nInput = {}
	local bingoPos = {}
	local bingoGame = ""
	local totalAmount = 0
	local butPower = 0
	local fixedBonus = {}
	local variableBonus = {}
	local nTotalMoney = 0
	local jItem
	local nCancelMatchNum = 0

	temp = Split(inStr,"#B")
	if tonumber( temp[2] ) then butPower = tonumber( temp[2] ) else butPower = 1 end

	sxItem = Split(temp[1],"|")


	equalLotItem = equalSplit(sxItem[2])
	jItem = Split(bingoStr, "#")
	equalBingoItem = equalSplit(jItem[1])
	nTotalMoney = tonumber(jItem[2])

	for i = 1, #equalBingoItem do
		temp = Split(equalBingoItem[i][2],":")
		Host[i] = tonumber(temp[1])
		Guest[i] = tonumber(temp[2])
		concedePoint[i] = tonumber(temp[3])
		wholePoint[i] = tonumber(temp[4])
		Sp[i] = tonumber(temp[5])
	end

	for i = 1,#equalLotItem do
		for j = 1, #equalBingoItem do
			if string.find(equalLotItem[i][1], equalBingoItem[j][1]) ~= nil then
				pos[i] = j
				break
			end
		end
	end




	if butType == '1' or butType == '2' then
		k = 1
		for i = 1, #equalLotItem do
			if butType == '2'then
				nTemp = Host[pos[i]] + concedePoint[pos[i]] - Guest[pos[i]]
			else
				nTemp = Host[pos[i]] - Guest[pos[i]]
			end
			if  nTemp > 0 then
				bingo = 3
			else
				bingo = 0
			end
			if string.find(equalLotItem[i][2],bingo) ~= nil or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
				k = k + 1
			else
				nInput[i] = -1
			end
		end
	elseif butType == '3' then
		k = 1
		for i = 1, #equalLotItem do
			point = Host[pos[i]] - Guest[pos[i]]
			bingo = computeSFC(point)
			if string.find(equalLotItem[i][2], bingo) ~= nil or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
				k = k + 1
			else
				nInput[i] = -1
			end

		end
	elseif butType == '4' then
		k = 1
		for i = 1, #equalLotItem do
			bingo = Host[pos[i]] + Guest[pos[i]] - wholePoint[pos[i]]
			if bingo > 0 then
				bingo = 3
			else
				bingo = 0
			end


			if string.find(equalLotItem[i][2], bingo) ~= nil or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
				k = k + 1
			else
				nInput[i] = -1
			end

		end
	elseif	butType == '5' then
		k = 1
		for i = 1, #equalLotItem do
			jhItem = Split(equalLotItem[i][1], ">")
			if jhItem[1] == "SF" or jhItem[1] == "RFSF" then
				nTemp = Host[pos[i]] + concedePoint[pos[i]] - Guest[pos[i]]
				if  nTemp > 0 then
					bingo = 3
				else
					bingo = 0
				end
			elseif jhItem[1] == "SFC" then
				point = Host[pos[i]] - Guest[pos[i]]
				bingo = computeSFC(point)
			elseif jhItem[1] == "DXF" then
				bingo = Host[pos[i]] + Guest[pos[i]] - wholePoint[pos[i]]
				if bingo > 0 then
					bingo = 3
				else
					bingo = 0
				end
			end
			if string.find(equalLotItem[i][2],bingo) ~= nil or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
				k = k + 1
			else
				nInput[i] = -1
			end
		end
	end

	nTotal = #equalLotItem
	xItem = Split(sxItem[3], "*")
	m = tonumber(xItem[1])
	n = tonumber(xItem[2])
	temp = 0
	position = PosMapping[sxItem[3]]
	flag = 1
	if position  == nil then
		flag = 0
		for i = 1, m do
			temp = temp + combination(m, m + 1 - i)
			if temp == n then
				position  = m + 1 - i
				break
			end
		end
	end

       if(sxItem[3] == '6*35') then
		position = 2
		m = 3
	elseif(sxItem[3] == '6*50') then
		position = 2
		m = 4
	elseif(sxItem[3] == '5*20') then
		position = 2
		m = 3
	end

	if nCancelMatchNum == m then
		totalAmount = nTotalMoney
	elseif sxItem[3] == '1*1'  then
		for i = 1, #bingoPos do
			if nInput[bingoPos[i]] > 5000000 then
				totalAmount = totalAmount + 5000000*butPower
			elseif 2*nInput[bingoPos[i]] < 2 then
				totalAmount = totalAmount + 2*butPower
			else
				totalAmount = totalAmount + 2*nInput[bingoPos[i]] * butPower
			end
		end
	else
		if flag == 0 then
			for i = position, m do
				totalAmount = totalAmount + ComputeJCZQMoney(nInput, nTotal, i, {} )
			end

		elseif flag == 1 then
			totalAmount = totalAmount + ComputeJCZQMoney(nInput, nTotal, position, {})

		end
		totalAmount = totalAmount * butPower
	end

	--竞彩奖金计算，要单倍四舍五入再乘以倍数
	totalAmount = math.floor((totalAmount/butPower)*100+0.5)/100*butPower

	for i = 1, #bingoPos do
		bingoGame = bingoGame..equalBingoItem[bingoPos[i]][1]
		if i < #bingoPos then bingoGame = bingoGame.."," end
	end




	return totalAmount , bingoGame

end




--print(check_jclq( '1', "130912304=0,130912301=3,130912302=3,130912303=0,130912305=0|5*1#B1"))
--print(check_lc( '5', "SF>111103301=3,RFSF>111103302=0,DXF>111103303=3,SFC>111103304=05,SF>111103305=3|5*10"))

--竞彩篮球测试兑奖
--print(query_jclq('2', "RFSF|150225305=3|1*1#B50", "150225305=110:105:-3.5:218.5:1.750#100"))
